<template>
  <div class="goodslist">
    <div
      class="goods-item"
      v-for="(item,index) in goodslist"
      :key="index"
      @click="godetail(item.id)"
    >
      <img :src="item.img_url" alt>
      <h1 class="title">{{ item.title }}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.sell_price }}</span>
          <span class="old">￥{{ item.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>

    <van-button
      type="danger"
      size="large"
      :disabled="flag"
      @click="getMore"
    >{{hasFlag ? ' 被掏空了': '加载更多'}}</van-button>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      goodslist: [],
      // 请求第几页的数据
      pageindex: 0,
      // 默认每页显示条数
      limit: 3,
      hasFlag: false,
      flag: false
    };
  },
  created() {
    this.getgoods();
  },
  methods: {
    /**
     * 获取goodslist数据
     */
    async getgoods() {
      if (this.hasFlag) {
        // this.$refs.btn.disabled = true;
        this.flag = true;
        return;
      }
      this.pageindex++;
      const {
        data: { status, message, count }
      } = await this.$http.get(
        `api/getgoods?pageindex=${this.pageindex}&limit=${this.limit}`
      );
      if (status === 0) {
        this.goodslist = this.goodslist.concat(message);
        // 计算一个布尔值 作为加载更多数据的切换
        this.hasFlag = this.pageindex * this.limit > count;
      } else {
        Toast("获取goodslist数据失败");
      }
    },
    getMore() {
      this.getgoods();
    },
    godetail(id) {
      this.$router.push("/home/goodsinfo/" + id);
      // this.$route    // 路由规则参数    
      // this.$router   // 路由对象
      // 字符串
      // this.$router.push('/home/goodsinfo?id=1')
      // this.$router.push("/home/goodsinfo/1");
      // 对象
      // this.$router.push({ path: "/home/goodsinfo?id=1" });
      // this.$router.push({ path: "/home/goodsinfo/1" });
      // 命名的路由
      // name 值来至于路由规则里面定义的 name值

      // this.$router.push({ name: "goodsinfo", params: { id: 1 } });  // 路由规则需要占位   如： {name:'goodsinfo',path:'/home/goodsinfo/:id'}
      // 带查询参数，变成 /register?plan=private
      // this.$router.push({ name: "goodsinfo", query: { id: 1 } }); // 路由规则不要占位
    }
  }
};
</script>
<style lang="less">
.goodslist {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  // 两边对齐中间留空格
  justify-content: space-between;

  .goods-item {
    width: 48%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    // 默认x 轴
    // 设置为column 改为 y轴
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }

    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
}
</style>
